package com.example.yuekaoceshi.fragment;


import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.yuekaoceshi.MainActivity;
import com.example.yuekaoceshi.R;
import com.example.yuekaoceshi.adapter.RlvAdapter;
import com.example.yuekaoceshi.bean.Bean;
import com.example.yuekaoceshi.bean.Info;
import com.example.yuekaoceshi.http.DataBaseUtils;
import com.example.yuekaoceshi.http.TypeApi;
import com.example.yuekaoceshi.moudle.Moudle;
import com.example.yuekaoceshi.presenter.Presenter;
import com.example.yuekaoceshi.view.Main_view;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class ListFragment extends Fragment implements Main_view{


    private View view;
    private XRecyclerView mRlv;
    RlvAdapter mRlvAdapter;
    private int mPosition;
    ArrayList<Bean.RESULTBean> mList;

    public ListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_list, container, false);
        data();
        ActivityCompat.requestPermissions(getActivity(),new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_COARSE_LOCATION
        },1);
        initView(inflate);
        return inflate;
    }

    private void data() {
        Presenter presenter = new Presenter(new Moudle(),this);
        HashMap<String, Object> map = new HashMap<>();
        map.put("appKey","1830dd35e90e4052874a189c56736704");
        presenter.getdata("prov",map,null, TypeApi.RLV);
    }

    private void initView(View inflate) {
        mRlv = (XRecyclerView) inflate.findViewById(R.id.rlv);
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRlv.setLayoutManager(layoutManager);
        mRlvAdapter = new RlvAdapter(mList);
        mRlv.setAdapter(mRlvAdapter);
        mRlv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mRlv.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                mRlv.refreshComplete();
            }
        });
        //长按监听
        mRlvAdapter.setOnItemClickListener(new RlvAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View v, int position) {
                mPosition=position;
            }
        });
        //点击监听
        mRlvAdapter.setOnItemLongClickListener(new RlvAdapter.OnItemLongClickListener() {
            @Override
            public void OnItemLongClick(View v, int position) {
                UMImage image = new UMImage(getContext(), R.drawable.as);//网络图片
                new ShareAction((Activity) getContext()).withText("asd").withExtra(image).setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
                        .setCallback(new UMShareListener() {
                            @Override
                            public void onStart(SHARE_MEDIA share_media) {

                            }

                            @Override
                            public void onResult(SHARE_MEDIA share_media) {
                                Log.e("liangxq", "onResult");
                            }

                            @Override
                            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                                Log.e("liangxq", throwable.getMessage());
                            }

                            @Override
                            public void onCancel(SHARE_MEDIA share_media) {

                            }
                        }).open();
            }
        });
        registerForContextMenu(mRlv);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(1,1,1,"收藏");
        menu.add(1,2,2,"修改");
        menu.add(1,3,3,"删除");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                collect();
                break;
            case 2:
                break;
            case 3:
                delect();
                break;
        }
        return super.onContextItemSelected(item);
    }

    private void delect() {
        mList.remove(mPosition);
        mRlvAdapter.notifyDataSetChanged();
    }

    private void collect() {
        Bean.RESULTBean bean = mList.get(mPosition);
        ArrayList<Info> infos = new ArrayList<>();
       if (mPosition%2==0){
           infos.add(new Info(null,R.drawable.aaaa,bean.getProName()));
       }else {
           infos.add(new Info(null,R.drawable.timg,bean.getProName()));
       }
        DataBaseUtils.getInstance().insert(infos);
    }

    @Override
    public void showData(Object o, TypeApi typeApi) {
        Log.e("duan", "showData: ========"+o );
        String s= (String) o;
        Gson gson = new Gson();
        Bean bean = gson.fromJson(s, Bean.class);
        List<Bean.RESULTBean> list = bean.getRESULT();
        if (list.size()>0){
            mRlvAdapter.addData(list);
        }
    }

    @Override
    public void showError(String error) {

    }

}
